package com.boom.springapp.javafxdemo.common;

import com.alibaba.fastjson2.JSON;
import com.boom.springapp.javafxdemo.config.ConfigConstant;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author hhl
 * @version 1.0
 * @description
 * @date 2025/11/19 21:55
 */
@Slf4j
@Data
@Component
public class DefaultConfiguration {
    // 基础设置
    public String cookie = "";
    /**
     * 默认输出目录
     */
    public String defaultOutputPath = "";

    /**
     * 默认评论顺序
     */
    public Integer defaultCommentOrder = CommentOrder.LIKE.getOrder();

    /**
     * 默认视频排序
     */
    public Integer defaultVideoOrder = VideoOrder.PUBLISH_TIME.getOrder();

    /**
     * 默认生成评论地区分布地图
     */
    public Boolean defaultGenerateMap = true;

    /**
     * 下载评论时自动获取图片
     */
    public Boolean defaultDownloadImage = true;

    // 请求延迟和重试设置

    /**
     * 最小请求延迟（秒）
     */
    public Double minDelay = 1.0;

    /**
     * 最大请求延迟（秒）
     */
    public Double maxDelay = 2.0;

    /**
     * 重试等待时间（秒）
     */
    public Double retryDelay = 5.0;

    /**
     * 最大重试次数
     */
    public Integer maxRetryCount = 2;

    /**
     * 连续空页面限制
     */
    public Integer emptyPageLimit = 1;

    public void saveConfig() {
        String configPath = ConfigConstant.CONFIG_FILE_PATH + "config.json";
        String jsonString = JSON.toJSONString(this);
        try {
            Files.write(
                    Paths.get(configPath),
                    jsonString.getBytes(java.nio.charset.StandardCharsets.UTF_8)
            );
            log.info("配置已保存至: {}", configPath);
        } catch (java.io.IOException e) {
            log.error("保存配置文件失败: {}", e.getMessage(), e);
        }
    }

    @PostConstruct
    public void loadConfig() {
        String configPath = ConfigConstant.CONFIG_FILE_PATH + "config.json";
        try {
            if (!Files.exists(Paths.get(configPath))) {
                Files.createFile(Paths.get(configPath));
            }
            byte[] bytes = Files.readAllBytes(Paths.get(configPath));
            DefaultConfiguration config = JSON.parseObject(new String(bytes), DefaultConfiguration.class);
            if (config == null){
                log.warn("配置文件不存在，已创建一个默认配置文件 {}", configPath);
                return;
            }
            this.cookie = config.cookie;
            this.defaultOutputPath = config.defaultOutputPath;
            this.defaultCommentOrder = config.defaultCommentOrder;
            this.defaultVideoOrder = config.defaultVideoOrder;
            this.defaultGenerateMap = config.defaultGenerateMap;
            this.defaultDownloadImage = config.defaultDownloadImage;
            this.minDelay = config.minDelay;
            this.maxDelay = config.maxDelay;
            this.retryDelay = config.retryDelay;
            this.maxRetryCount = config.maxRetryCount;
            this.emptyPageLimit = config.emptyPageLimit;
            log.info("配置已加载: {}", JSON.toJSONString(config));
            log.info("配置详情 - 输出路径: {}, 生成地图: {}, 下载图片: {}, 评论排序: {}, 视频排序: {}, Cookie长度: {}, 最小延迟: {}, 最大延迟: {}, 重试延迟: {}, 最大重试次数: {}, 空页面限制: {}",
                    this.defaultOutputPath, this.defaultGenerateMap, this.defaultDownloadImage,
                    this.defaultCommentOrder, this.defaultVideoOrder,
                    this.cookie != null ? this.cookie.length() : 0,
                    this.minDelay, this.maxDelay, this.retryDelay, this.maxRetryCount, this.emptyPageLimit);
        } catch (java.io.IOException e) {
            log.error("加载配置文件失败: {}", e.getMessage(), e);
        }
    }
}
